package com.west.xu.jdbc.utils;

import com.west.xu.jdbc.annotation.BeanFieldToDB;
import com.west.xu.jdbc.base.DBJsonType;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class StringUtilTemporary {
    /**
     * 获取对象全部属性名数组
     */
    public static String[] getFiledNames(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> fieldNameList = new ArrayList<>();
        for (Field field : fields) {
            fieldNameList.add(field.getName());
        }
        return fieldNameList.toArray(new String[fieldNameList.size()]);
    }

    /**
     * 根据属性名获取属性值
     */
    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter);
            return method.invoke(o);
        } catch (Exception e) {
            return "";
        }

    }

    public static InputStream getFieldListToStream(List<String> jsonList, List<String> listField, Type clazz) {
        try {
            StringBuilder builder = new StringBuilder();
            Object object;
            Object value;
            for (String aJsonList : jsonList) {
                object = JsonParseUtil.parseObject(aJsonList, clazz);
                for (int i = 0; i < listField.size(); i++) {
                    value = StringUtilTemporary.getFieldValueByName(listField.get(i), object);
                    if (value instanceof String) {
                        builder.append("'").append(value).append("'");
                    } else {
                        builder.append(value);
                    }
                    if (i == listField.size() - 1) {
                        builder.append("\n");
                    } else {
                        builder.append("\t");
                    }
                }
            }
            byte[] bytes = builder.toString().getBytes();
            return new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getFieldListToString(List<String> jsonList, List<String> listField, Type clazz) {
        try {
            if (jsonList.size() == 0) {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            Object object;
            Object value;
            for (String aJsonList : jsonList) {
                object = JsonParseUtil.parseObject(aJsonList, clazz);
                builder.append("(");
                for (int i = 0; i < listField.size(); i++) {
                    value = StringUtilTemporary.getFieldValueByName(listField.get(i), object);
                    if (listField.get(i).equals("id")) {
                        if (!ValidateUtil.objectIsNotNull(value)) {
                            value = UUID.randomUUID().toString().replaceAll("-", "");
                        }
                    }
                    if (value instanceof String) {
                        builder.append("'").append(value).append("'");
                    } else if (value instanceof List) {
                        builder.append("'").append(JsonParseUtil.toJSONString(value)).append("'");
                    } else if (value instanceof DBJsonType) {
                        builder.append("'").append(JsonParseUtil.toJSONString(value)).append("'");
                    } else {
                        builder.append(value);
                    }
                    if (i == listField.size() - 1) {
                        builder.append("),");
                    } else {
                        builder.append(",");
                    }
                }
            }
            return builder.substring(0, builder.length() - 1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> String getFieldListToString(List<T> list, List<String> listField) {
        try {
            if (list.size() == 0) {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            Object value;
            for (T item : list) {
                builder.append("(");
                for (int i = 0; i < listField.size(); i++) {
                    value = StringUtilTemporary.getFieldValueByName(listField.get(i), item);
                    if (listField.get(i).equals("id")) {
                        if (!ValidateUtil.objectIsNotNull(value)) {
                            value = UUID.randomUUID().toString().replaceAll("-", "");
                        }
                    }
                    if (value instanceof String) {
                        builder.append("'").append(value).append("'");
                    } else if (value instanceof List) {
                        builder.append("'").append(JsonParseUtil.toJSONString(value)).append("'");
                    } else if (value instanceof DBJsonType) {
                        builder.append("'").append(JsonParseUtil.toJSONString(value)).append("'");
                    } else {
                        builder.append(value);
                    }
                    if (i == listField.size() - 1) {
                        builder.append("),");
                    } else {
                        builder.append(",");
                    }
                }
            }
            return builder.substring(0, builder.length() - 1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取对象入库属性名数组(插入属性)
     */
    public static String[] getTableInsertColumns(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> fieldNameList = new ArrayList<>();
        for (Field field : fields) {
            if (field.isAnnotationPresent(BeanFieldToDB.class)) {
                BeanFieldToDB beanFieldToDB = field.getAnnotation(BeanFieldToDB.class);
                if (beanFieldToDB != null) {
                    if (beanFieldToDB.insertField()) {
                        fieldNameList.add(field.getName());
                    }
                }
            }
        }
        return fieldNameList.toArray(new String[fieldNameList.size()]);
    }

    /**
     * 获取对象入库属性名数组(更新属性)
     */
    public static String[] getTableUpdateColumns(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> fieldNameList = new ArrayList<>();
        for (Field field : fields) {
            if (field.isAnnotationPresent(BeanFieldToDB.class)) {
                BeanFieldToDB beanFieldToDB = field.getAnnotation(BeanFieldToDB.class);
                if (beanFieldToDB != null) {
                    if (beanFieldToDB.updateField()) {
                        fieldNameList.add(field.getName());
                    }
                }
            }
        }
        return fieldNameList.toArray(new String[fieldNameList.size()]);
    }
}
